package com.gxuwz.business.action;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;

import com.gxuwz.business.entity.Student;
import com.gxuwz.business.entity.SysUser;
import com.gxuwz.business.entity.Treat;
import com.gxuwz.business.service.IStudentService;
import com.gxuwz.business.service.ITreatService;
import com.gxuwz.util.Pager;
import com.gxuwz.util.TimeSplitUtils;
import com.gxuwz.util.UidUtils;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

import net.sf.json.JSONObject;

public class TreatAction extends BaseAction implements Preparable,ModelDriven<Treat>{


	private static final long serialVersionUID = -382091672603965110L;
	
	private static final String TREAT_ADD = "/WEB-INF/page/treat/treat_add.jsp";
	private static final String TREAT_LIST = "/WEB-INF/page/treat/treat_list.jsp";
	
	private Treat treat;
	private Student student = new Student();
	private SysUser user = new SysUser();
	private Pager<Treat> treatList;//分页数据
	
	@Autowired
	private ITreatService treatService;
	@Autowired
	private IStudentService studentService;
	
	/**
	 * 打开诊疗信息页面
	 */
	public String treatList(){
		forwardView = TREAT_LIST;
		return SUCCESS;
	}
	
	/**
	 * 分页查询挂号信息
	 */
	public String getByPager(){
		//时间段查询
		if(null != timeSection && !"".equals(timeSection)){
			dateStringList = TimeSplitUtils.timeSplitToString(timeSection);
			String openTime = dateStringList.get(0);
			String endTime = dateStringList.get(1);
		}
		treatList = treatService.getByPager(treat, page, limit);
		JsonData = JSONObject.fromObject(treatList);
		System.out.println(JsonData);
		return "JsonObject";
	}
	
	/**
	 * 打开添加诊疗页面
	 */
	public String openAdd(){
		//获取用户
		user = (SysUser)getSession().getAttribute("user");
		//获取挂号信息
		student.setRegId(treat.getRegId());
		student = studentService.findById(student);
		
		forwardView = TREAT_ADD;
		return SUCCESS;
	}
	
	/**
	 * 添加诊疗信息
	 */
	public String add(){
		treat.setTreatId(UidUtils.UIDD());
		treat.setTreatDate(new Date());
		treat.setTreatCount(0);
		treat.setStatus(0);
		//等待处方状态
		treat.setState(0);
		//将挂号表的状态置为治疗中
		studentService.updateState(treat.getRegId(), 1);
		isSucceed = treatService.add(treat);
		jsonString = jsonToString(true);
		return "JsonString";
	}
	
	
	
	@Override
	public Treat getModel() {
		return treat;
	}

	@Override
	public void prepare() throws Exception {
		if(null == treat){
			treat = new Treat();
		}
	}

	public Student getStudent() {
		return student;
	}

	public void setStudent(Student student) {
		this.student = student;
	}

	public SysUser getUser() {
		return user;
	}

	public void setUser(SysUser user) {
		this.user = user;
	}
	
	

}
